<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace App\Model;

use Think\Model\RelationModel;
use App\Model\AnswerModel;

/**
 * Class QuestionModel
 * @package Home\Model
 */
class QuestionModel extends RelationModel {


    public function __construct() {
        parent::__construct();
        $this->answerModel = new AnswerModel();
        $this->user_id = session('user_id');
    }

    public function getInfoById($id) {
        $id = $id ? $id : 0;
        $find = M('question')->alias('q')->join('left join __USERS__ as u on q.user_id = u.user_id and u.is_lock = 0')
            ->field('q.*,u.username,u.mobile,u.sex,u.head_pic')
            ->where('q.id = '.$id.' ')->find();
        if($find){
            $find['head_pic'] = (strpos($find['head_pic'],'http') === false && $find['head_pic']) ? SITE_URL.$find['head_pic'] : $find['head_pic'];
            $find['cats_name'] = array_values(getCatName($find['cat_ids']));
            $find['response_sec_desc'] = cacTime($find['response_sec']);
            $find['question_status_desc'] = getQuestionStatus($find['question_status']);
            $find['answer_status_desc'] = getAnswerStatus($find['answer_status']);
            $find['stop_time'] = date('Y-m-d H:i',$find['stop_time']);
            $find['add_time'] = date('Y-m-d H:i',$find['add_time']);
        }else{
            return false;
        }
        return $find;
    }

    public function getList($state = '',$page = 1,$limit = 6,$cac_count = false) {
        if($state == 1){ //已回答
            $select = M('answer')->alias('a')
                ->join('left join __QUESTION__ as q on a.question_id = q.id')
                ->join('left join __USERS__ as u on q.user_id = u.user_id and u.is_lock = 0')
                ->field('q.id,q.title,q.add_time,q.cat_ids,u.head_pic,u.username,a.charge_view,a.charge_money,a.charge_view,q.open_reward,q.reward_money,q.answer_status')
                ->where('a.accountant_id = '.$this->user_id.' and q.answer_status = 2');
        }else{ //未回答
            $now = time();
            $select = M('question_answer')->alias('qa')
                ->join('left join __QUESTION__ as q on qa.question_id = q.id')
                ->join('left join __USERS__ as u on q.user_id = u.user_id and u.is_lock = 0')
                ->field('q.id,q.title,q.add_time,q.cat_ids,u.head_pic,u.username,
                q.open_reward,q.reward_money,q.answer_status')
                ->where('qa.answer_id = '.$this->user_id.' and q.answer_status = 1 and q.question_status != 1 and q.stop_time > '.$now);
        }

        if($cac_count){
            return $select->count();
        }
        $select = $select->page($page,$limit)->order('q.add_time desc')->select();
        if($select){
            $arr = array();
            foreach ($select as &$find) {

                $temp['respondent_head'] = (strpos($find['head_pic'],'http') === false && $find['head_pic']) ? SITE_URL.$find['head_pic'] : $find['head_pic'];
                $temp['respondent_name'] = $find['username'] ;
                $temp['id'] = $find['id'] ;
                $temp['problemTitle'] = $find['title'] ;
                //$temp['price'] = $find['charge_view'] ? $find['charge_money'] : 0;
                $temp['price'] = $find['open_reward'] ? $find['reward_money'] : 0;
                $temp['money'] = $find['charge_view'] ? $find['charge_money'] : 0;
                $temp['state'] = $find['answer_status'] == 2 ? 1 : 0;
                $temp['state_desc'] = getAnswerStatus($find['answer_status']);
                $temp['professionalList'] = array_values(getCatName($find['cat_ids']));
                $temp['time'] = date('Y-m-d H:i', $find['add_time']);

                $arr[] = $temp;
            }
        }else{
            return false;
        }
        return $arr;
    }



    public function getQuestionByIdWithAnswer($id) {
        $id = $id ? $id : 0;
        $find = $this->getInfoById($id);
        if($find) {
            //M('question')->where('id = '.$id)->setInc('read_num' , 1); //+1
            //是否已经支付悬赏
            if ($find['question_status'] != 1) {
                $find['answer_arr'] = $this->answerModel->getInfoByQuestionId($find['id']);
            }
            return $find;
        }
        return false;
    }


}
